<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form class="-mb-15px" :model="queryParams" ref="queryFormRef" :inline="true">
      <el-form-item :label="t('student.fields.batchId')" prop="batchId">
        <el-select
          v-model="queryParams.batchId"
          :placeholder="t('common.selectTextPlaceholder') + t('student.fields.batchId')"
          clearable
          class="!w-160px"
          @change="handleQuery"
        >
          <el-option
            v-for="(item, index) in batchOptions"
            :key="index"
            :label="item.batchName"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item :label="t('student.fields.studentName')" prop="studentName">
        <el-input
          v-model="queryParams.studentName"
          :placeholder="t('common.inputTextPlaceholder') + t('student.fields.studentName')"
          clearable
          @keyup.enter="handleQuery"
          class="!w-160px"
        />
      </el-form-item>
      <el-form-item :label="t('student.fields.chioceStatus')" prop="chioceStatus">
        <el-select
          v-model="queryParams.chioceStatus"
          :placeholder="t('common.selectTextPlaceholder') + t('student.fields.chioceStatus')"
          clearable
          class="!w-160px"
          @change="handleQuery"
        >
          <el-option :label="t('student.index.dcUnoperate')" value="未操作" />
          <el-option :label="t('student.index.dcNotChosen')" value="未双选" />
          <el-option :label="t('student.index.dcChosen')" value="已双选" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button @click="handleQuery">
          <Icon icon="ep:search" class="mr-5px" />
          {{ t('common.search') }}
        </el-button>
        <el-button @click="resetQuery">
          <Icon icon="ep:refresh" class="mr-5px" />
          {{ t('common.reset') }}
        </el-button>
        <SendChatRecruit
          :list="selectListObject"
          :title="t('doubleChioce.index.smsReminder')"
          default-message="您的研究生招生面试已通过。请尽快登录中国科学院金属研究所公众号选择导师双选。"
        />
        <el-button type="primary" @click="handleViewRecall">
          <Icon icon="ep:checked" class="mr-5px" />
          {{ t('doubleChoiceRecall.audit.btn') }}
        </el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <vxe-toolbar ref="toolbarRef" custom />
    <vxe-table
      id="iDoubleChioceStudent-table"
      ref="tableRef"
      v-loading="loading"
      :data="list"
      stripe
      :custom-config="vxeTableConfig"
      :column-config="{ resizable: true }"
      :size="vxeTableSize"
      @checkbox-all="collectSelect"
      @checkbox-change="collectSelect"
    >
      <vxe-column type="checkbox" width="50" align="center" fixed="left" />
      <vxe-column
        :title="t('student.fields.studentName')"
        field="studentName"
        align="center"
        show-header-overflow
        show-overflow
        min-width="100"
        fixed="left"
      />
      <vxe-column
        :title="t('student.fields.recruitType')"
        field="recruitType"
        align="center"
        show-header-overflow
        show-overflow
        min-width="100"
      />
      <vxe-column
        :title="t('student.fields.phone')"
        field="phone"
        align="center"
        show-header-overflow
        show-overflow
        min-width="100"
      />
      <vxe-column
        :title="t('student.fields.batchId')"
        field="batchId"
        align="center"
        show-header-overflow
        show-overflow
        min-width="100"
      >
        <template #default="scope">
          {{ batchOptions.find((item) => item.id === scope.row.batchId)?.batchName }}
        </template>
      </vxe-column>
      <vxe-column
        :title="t('student.fields.chioceStatus')"
        field="chioceStatus"
        align="center"
        show-header-overflow
        show-overflow
        min-width="100"
      />
      <vxe-column
        :title="t('student.fields.chioceMentorId')"
        field="mentorName"
        align="center"
        show-header-overflow
        show-overflow
        min-width="100"
      />
      <vxe-column
        :title="t('student.fields.interviewScore')"
        field="interviewScore"
        align="center"
        show-header-overflow
        show-overflow
        min-width="100"
      />
      confirmJudges
      <vxe-column
        :title="t('student.fields.confirmJudges')"
        field="confirmJudges"
        align="center"
        show-header-overflow
        show-overflow
        min-width="100"
      />
      <vxe-column
        :title="t('student.fields.birthdate')"
        field="birthdate"
        align="center"
        show-header-overflow
        show-overflow
        min-width="100"
        :formatter="vxeDateFormatter2"
      />
      <vxe-column
        :title="t('student.fields.gender')"
        field="gender"
        align="center"
        show-header-overflow
        show-overflow
        min-width="100"
      />
      <vxe-column
        :title="t('student.fields.politicalStatus')"
        field="politicalStatus"
        align="center"
        show-header-overflow
        show-overflow
        min-width="100"
      />
      <vxe-column
        :title="t('student.fields.ethnic')"
        field="ethnic"
        align="center"
        show-header-overflow
        show-overflow
        min-width="100"
      />
      <vxe-column
        :title="t('student.fields.identityNumber')"
        field="identityNumber"
        align="center"
        show-header-overflow
        show-overflow
        min-width="100"
      />
      <vxe-column
        :title="t('student.fields.contactAddress')"
        field="contactAddress"
        align="center"
        show-header-overflow
        show-overflow
        min-width="100"
      />
      <vxe-column
        :title="t('student.fields.email')"
        field="email"
        align="center"
        show-header-overflow
        show-overflow
        min-width="100"
      />
      <vxe-column
        :title="t('student.fields.university')"
        field="university"
        align="center"
        show-header-overflow
        show-overflow
        min-width="100"
      />
      <vxe-column
        :title="t('student.fields.is985')"
        field="is985"
        align="center"
        show-header-overflow
        show-overflow
        min-width="100"
        :formatter="vxeBooleanFormatter"
      />
      <vxe-column
        :title="t('student.fields.is211')"
        field="is211"
        align="center"
        show-header-overflow
        show-overflow
        min-width="100"
        :formatter="vxeBooleanFormatter"
      />
      <vxe-column
        :title="t('student.fields.isFirstClass')"
        field="isFirstClass"
        align="center"
        show-header-overflow
        show-overflow
        min-width="100"
        :formatter="vxeBooleanFormatter"
      />
      <vxe-column
        :title="t('student.fields.studyMajor')"
        field="studyMajor"
        align="center"
        show-header-overflow
        show-overflow
        min-width="100"
      />
      <vxe-column
        :title="t('student.fields.applyMajor')"
        field="applyMajor"
        align="center"
        show-header-overflow
        show-overflow
        min-width="100"
      />
      <vxe-column
        :title="t('student.fields.majorRank')"
        field="majorRank"
        align="center"
        show-header-overflow
        show-overflow
        min-width="100"
      />
      <vxe-column
        :title="t('student.fields.majorTotal')"
        field="majorTotal"
        align="center"
        show-header-overflow
        show-overflow
        min-width="100"
      />
      <vxe-column
        :title="t('student.fields.classRank')"
        field="classRank"
        align="center"
        show-header-overflow
        show-overflow
        min-width="100"
      />
      <vxe-column
        :title="t('student.fields.classTotal')"
        field="classTotal"
        align="center"
        show-header-overflow
        show-overflow
        min-width="100"
      />
      <vxe-column
        :title="t('student.fields.cet4Score')"
        field="cet4Score"
        align="center"
        show-header-overflow
        show-overflow
        min-width="100"
      />
      <vxe-column
        :title="t('student.fields.cet6Score')"
        field="cet6Score"
        align="center"
        show-header-overflow
        show-overflow
        min-width="100"
      />
      <vxe-column
        :title="t('student.fields.attachment')"
        field="attachment"
        align="center"
        show-header-overflow
        show-overflow
        min-width="100"
      >
        <template #default="scope">
          <FileObjectDownload :file-json="scope.row.attachment" />
        </template>
      </vxe-column>
      <vxe-column
        :title="t('student.fields.remark')"
        field="remark"
        align="center"
        show-header-overflow
        show-overflow
        min-width="100"
        :visible="false"
      />
      <vxe-column
        :title="t('student.fields.createTime')"
        field="createTime"
        align="center"
        show-header-overflow
        show-overflow
        min-width="100"
        :formatter="vxeDateFormatter"
        :visible="false"
      />
      <vxe-column
        :title="t('common.operate')"
        field="operator"
        align="center"
        fixed="right"
        width="190"
      >
        <template #default="scope">
          <el-button link type="primary" @click="handleViewChoice(scope.row)">
            {{ t('doubleChioce.index.viewChoice') }}
          </el-button>
          <el-button
            link
            type="primary"
            @click="handleBindMentor(scope.row)"
            :disabled="scope.row.chioceStatus === '已双选'"
          >
            {{ t('doubleChioce.index.bindMentor') }}
          </el-button>
        </template>
      </vxe-column>
    </vxe-table>
    <!-- 分页 -->
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>

  <DoubleChoiceDetail ref="choiceDetailRef" @revoked="getList" />
  <DoubleChoiceBindMentor ref="bindMentorRef" @success="getList" />
  <DoubleChoiceRecallAudit ref="recallAuditRef" @success="getList" />
</template>

<script setup lang="ts">
import { vxeDateFormatter, vxeDateFormatter2 } from '@/utils/formatTime'
import type { VxeTableInstance, VxeTablePropTypes, VxeToolbarInstance } from 'vxe-table'
import { useAppStore } from '@/store/modules/app'
import { RecruitStudentChoiceListRespVO, StudentApi, StudentVO } from '@/api/recruit/recruitstudent'
import { BatchApi, BatchVO } from '@/api/recruit/batch'
import DoubleChoiceDetail from '@/views/recruit/doublechioce/DoubleChoiceDetail.vue'
import DoubleChoiceBindMentor from '@/views/recruit/doublechioce/DoubleChioceBindMentor.vue'
import DoubleChoiceRecallAudit from '@/views/recruit/doublechoicerecall/DoubleChoiceRecallAudit.vue'
import SendChatRecruit from '@/components/SendChatRecruit/index.vue'
import FileObjectDownload from '@/components/FileObjectDownload/index.vue'
import { vxeBooleanFormatter } from '@/utils/formatter'

/** 双选 列表 */
defineOptions({ name: 'DoubleChoiceStudent' })

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const toolbarRef = ref<VxeToolbarInstance>()
const tableRef = ref<VxeTableInstance>()

const loading = ref(true) // 列表的加载中
const list = ref<RecruitStudentChoiceListRespVO[]>([]) // 列表的数据
const total = ref(0) // 列表的总页数
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  batchId: undefined,
  studentName: undefined,
  chioceStatus: undefined
})
const queryFormRef = ref() // 搜索的表单

const vxeTableConfig: VxeTablePropTypes.CustomConfig = {
  storage: true,
  mode: 'drawer',
  checkMethod({ column }) {
    return !['seq', 'operator'].includes(column.field)
  }
}

const appStore = useAppStore()
const vxeTableSize = computed(() => appStore.getCurrentSizeVxe)

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await StudentApi.getStudentChoicePage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

// 双选详情弹窗
const choiceDetailRef = ref<InstanceType<typeof DoubleChoiceDetail>>()
const handleViewChoice = (row: RecruitStudentChoiceListRespVO) => {
  choiceDetailRef.value?.open(row.id)
}

// 直接绑定导师
const bindMentorRef = ref<InstanceType<typeof DoubleChoiceBindMentor>>()
const handleBindMentor = (row: RecruitStudentChoiceListRespVO) => {
  bindMentorRef.value?.open(row)
}

const batchOptions = ref<BatchVO[]>([])
const getBatchSelectOptions = async () => {
  batchOptions.value = await BatchApi.getSelectList()
}

const recallAuditRef = ref<InstanceType<typeof DoubleChoiceRecallAudit>>()
// 查看申请解除双选的学生
const handleViewRecall = () => {
  recallAuditRef.value?.open(queryParams.batchId)
}

const selectListObject = ref<StudentVO[]>([])
const collectSelect = () => {
  selectListObject.value = [
    ...(tableRef.value?.getCheckboxRecords() ?? []),
    ...(tableRef.value?.getCheckboxReserveRecords() ?? [])
  ]
}

/** 初始化 **/
onMounted(async () => {
  // 先查询所有计划
  await getBatchSelectOptions()
  // 然后查询最新计划的数据
  queryParams.batchId = batchOptions.value[0]?.id ?? undefined

  getList()

  const table = tableRef.value
  const toolbar = toolbarRef.value
  if (table && toolbar) {
    table.connect(toolbar)
  }
})
</script>

<style lang="scss" scoped>
:deep(.vxe-cell .el-button + .el-button) {
  margin-left: 0;
}

:deep(.el-form--inline .el-form-item) {
  margin-right: 15px !important;
}
</style>
